package com.example.demo.module.repository.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.entity.response.Rs;
import com.example.demo.constant.Constant;
import com.example.demo.entity.pojo.repository.PurchaseRepositoryPoJo;
import com.example.demo.entity.request.repository.PurchaseRepositoryQuery;
import com.example.demo.entity.request.repository.PurchaseRepositoryRequest;
import com.example.demo.entity.response.repository.PurchaseRepositoryResponse;
import com.example.demo.module.repository.service.PurchaseRepositoryService;
import com.example.demo.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * 采购单入库控制层
 *
 * @author Mei Ruoxiao
 * @date 2020/9/19
 */
@Slf4j
@Api(tags = "采购单入库表")
@RestController
@RequestMapping("purchase/repository")
public class PurchaseRepositoryController {
    @Resource
    private PurchaseRepositoryService service;

    /**
     * 获取采购单入库列表
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取采购单入库列表")
    public ResponseEntity<Rs<Page<PurchaseRepositoryResponse>>> page(
            @Valid @RequestBody PurchaseRepositoryQuery query,
            @ApiIgnore @RequestParam(value = Constant.HeaderKey.COMPANY_ID, required = false) long companyId) {
        log.info("page:获取采购单入库列表[request:{}, companyId:{}]", query, companyId);
        Page<PurchaseRepositoryPoJo> poJoPage = service.page(query, companyId);
        Page<PurchaseRepositoryResponse> responsePage = CommonUtils.toPage(poJoPage, PurchaseRepositoryResponse::of);
        return Rs.ok(CommonUtils.getResponse(responsePage));
    }


    /**
     * 入库
     */
    @RequestMapping("/warehouse")
    @ApiOperation(value = "入库")
    public ResponseEntity<Rs<Object>> warehouse(@RequestBody PurchaseRepositoryRequest request,
                                                @ApiIgnore @RequestParam(value = Constant.HeaderKey.COMPANY_ID, required = false) long companyId) {
        log.info("warehouse:入库[request:{}, companyId:{}]", request, companyId);
        return Rs.ok(service.warehouse(request, companyId));
    }

}
